package com.game.logic.items.dongFu;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.constant.AIDCFG;
import com.coment.cfg.luban.constant.CurrencyType;
import com.coment.cfg.luban.items.DongFuCfg;
import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import com.coment.po.UserDongFu;
import com.game.dao.user.UserPlayInfoDao;
import com.game.dao.user.dongfu.UserDongFuDao;
import com.game.dao.user.roles.RolesDao;
import com.game.framework.ActorManager;
import com.game.logic.fighting.attrs.AttrsManagement;
import com.game.logic.items.account.UserAccountActor;
import com.coment.message.account.PayInfo;
import com.xs.action.exection.ActionException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserDongFuService {
    @Resource
    private UserDongFuDao userDongFuDao;
    @Resource
    private AttrsManagement attrsManagement;
    @Autowired
    private RolesDao rolesDao;
    @Autowired
    private UserPlayInfoDao userPlayInfoDao;

    /// 查看洞府
    public UserDongFu get(Integer userInfoId) {
        UserDongFu userDongFu = userDongFuDao.findById(userInfoId).orElseGet(() -> {
            UserDongFu dongFu = userDongFuDao.findOrCreat(userInfoId);
            DongFuCfg next = Tables.cfg.get_tbdongfucfg().get(dongFu.getDongFuId());
            dongFu.setDongFuId(next.id);
            Attrs cfgAttrs = AttributeManagerLogic.obtainCfgAttrs(next);
            attrsManagement.logAttrsGlobal(userInfoId, cfgAttrs, "洞府");
            return dongFu;
        });
        userDongFu.setAttrs(attrsManagement.search(userInfoId, "洞府"));
        return userDongFu;
    }


    /// 升级洞府
    public Attrs upgrade(Integer userInfoId){
        UserDongFu dongFu = userDongFuDao.findOrCreat(userInfoId);

        DongFuCfg dongFuCfg = Tables.cfg.get_tbdongfucfg().get(dongFu.getDongFuId());

        if (dongFuCfg.next == null) throw new ActionException("没有下一级洞府");

        PayInfo payInfo = new PayInfo(userInfoId, CurrencyType.ls, dongFuCfg.need, "洞府升级");
        PayInfo result = ActorManager.ask(UserAccountActor.class, payInfo, userInfoId);

        if (!result.isPay()) {
            throw new ActionException(result.getMessage());
        }

        DongFuCfg next = Tables.cfg.get_tbdongfucfg().get(dongFuCfg.next);
        return replaceDongFu(userInfoId, dongFu, next);
    }

    public Attrs replaceDongFu(Integer userInfoId, UserDongFu dongFu, DongFuCfg next) {
        dongFu.setDongFuId(next.id);
        userDongFuDao.save(dongFu);


        Attrs cfgAttrs = AttributeManagerLogic.obtainCfgAttrs(next);
        Attrs attrs = attrsManagement.logAttrsGlobal(userInfoId, cfgAttrs, "洞府");

        String rolesId = userPlayInfoDao.findByUserInfoIdOrCreate(userInfoId).getRecentlyPlayedRolesId();
        Double cultivationSpeed = rolesDao.findByIdMust(rolesId).getRolePanel().getAttribute().get(AIDCFG.cultivationSpeed);
        attrs.put(AIDCFG.cultivationSpeed, cultivationSpeed);
        return attrs;
    }
}
